
package chapter3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

public class Ex9 extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Ex9();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new Ex9Panel();
    getContentPane().add(panel);
  }
}

class Ex9Panel extends JPanel {
  public Ex9Panel() {
    setPreferredSize(new Dimension(400, 400));
    setBackground(Color.white);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    int w = getWidth();
    int h = getHeight();
    float r = Math.min(w, h)/2;
    AffineTransform tx = new AffineTransform();
    tx.rotate(Math.atan2(2, 1));
    tx.scale(1, -1);
    tx.rotate(Math.atan2(-2, 1));
    Shape shape1 = new Rectangle2D.Double(0, 100, 100, 50);
    Shape shape2 = tx.createTransformedShape(shape1);
    g2.translate(w/4, h/4);
    g2.drawLine(-w/2, 0, w, 0);
    g2.drawLine(0, -h/2, 0, h);
    g2.setColor(Color.yellow);
    g2.drawLine(-h/4, -h/2, h/2, h);
    g2.setColor(Color.red);
    g2.draw(shape1);
    g2.setColor(Color.blue);
    g2.draw(shape2);
  }
}